package com.ruoyi.fjtseac.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.fjtseac.config.OpenIdConfig;
import com.ruoyi.fjtseac.domain.dto.UserDto;
import com.ruoyi.fjtseac.domain.po.User;
import com.ruoyi.fjtseac.service.UserService;
import com.ruoyi.fjtseac.util.GetPostUntil;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author icyrain11
 * @date 2023-06-29
 */

@Api(tags={"用户接口"})
@RestController
@RequestMapping("/user")
public class UserController extends BaseController
{
    @Autowired
    private UserService userService;


    /**
     * 查询用户列表
     */
    @GetMapping("/list")
    public TableDataInfo list(UserDto userDto)
    {
        startPage();
        List<User> list = userService.selectUserList(userDto);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserDto userDto)
    {
        List<User> list = userService.selectUserList(userDto);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @GetMapping(value = "/{account}")
    public AjaxResult getInfo(@PathVariable("account") Long account)
    {
        return AjaxResult.success(userService.selectUserByAccount(account));
    }

    /**
     * 新增用户
     */
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserDto userDto)
    {
        return toAjax(userService.insertUser(userDto));
    }

    /**
     * 修改用户
     */
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserDto userDto)
    {
        return toAjax(userService.updateUser(userDto));
    }

    /**
     * 删除用户
     */
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{accounts}")
    public AjaxResult remove(@PathVariable Long[] accounts)
    {
        return toAjax(userService.deleteUserByAccounts(accounts));
    }

    /**
     * 获取用户openId
     * @param code 用户授权码
     * @return
     */
    @GetMapping("/getOpenId")
    public AjaxResult getOpenId(String code) {

        return userService.getOpenId(code);
    }

}
